﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Util.Database;

namespace ConfigureNZBLoader
{
    public partial class EditDownloadedEpisodes : Form
    {
        public EditDownloadedEpisodes()
        {
            InitializeComponent();
        }

        private uint _ShowId;
        public uint ShowId
        {
            get { return _ShowId; }
            set { _ShowId = value; }
        }

        class DownloadedListAdapter
        {
            public string downloaddate;
            public uint season;
            public uint episode;
            public uint id;

            public DownloadedListAdapter(uint id, uint season, uint episode, string downloaddate)
            {
                this.id = id;
                this.season = season;
                this.episode = episode;
                this.downloaddate = downloaddate;
            }

            public override string ToString()
            {
                if (string.IsNullOrEmpty(downloaddate))
                {
                    return string.Format(System.Globalization.CultureInfo.InvariantCulture, "Season {0} Episode {1}", season, episode);
                }
                else
                {
                    return string.Format(System.Globalization.CultureInfo.InvariantCulture, "Season {0} Episode {1} - {2}", season, episode, downloaddate);
                }
            }
        }


        private void EditDownloadedEpisodes_Load(object sender, EventArgs e)
        {
            using (IDbCommand cmd = DB.CreateCommand())
            {
                cmd.CommandText = "select * from downloaded where id_show=? order by season, episode";
                IDataParameter par = cmd.CreateParameter();
                par.Value = ShowId;
                cmd.Parameters.Add(par);

                using (IDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        lstDownloaded.Items.Add(new DownloadedListAdapter((uint)(long)dr["ID"], (uint)(long)dr["SEASON"], (uint)(long)dr["EPISODE"], dr["DOWNLOADDATE"].ToString()));
                    }
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddDownloadedEpisode dlg = new AddDownloadedEpisode();
            if (DialogResult.OK == dlg.ShowDialog(this))
            {
                try
                {
                    uint Season = uint.Parse(dlg.Season, System.Globalization.CultureInfo.InvariantCulture);
                    uint Episode = uint.Parse(dlg.Episode, System.Globalization.CultureInfo.InvariantCulture);
                    using (IDbCommand cmd = DB.CreateCommand())
                    {
                        cmd.CommandText = "select count(*) from  downloaded where id_show=? and season=? and episode=?"; 
                        IDataParameter par = cmd.CreateParameter();
                        par.Value = ShowId;
                        cmd.Parameters.Add(par);
                        par = cmd.CreateParameter();
                        par.Value = Season;
                        cmd.Parameters.Add(par);
                        par = cmd.CreateParameter();
                        par.Value = Episode;
                        cmd.Parameters.Add(par);
                        if ((long)cmd.ExecuteScalar() == 0)
                        {
                            par = cmd.CreateParameter();
                            par.Value = dlg.DownloadAddress;
                            cmd.Parameters.Add(par);
                            cmd.CommandText = "insert into downloaded(id_show, season, episode, downloaddate, downloadaddress) values (?, ?, ?, datetime(), ?)";
                            cmd.ExecuteNonQuery();

                            cmd.Parameters.Clear();
                            cmd.CommandText = "select * from downloaded where id = (select max(id) from downloaded)";
                            using (IDataReader dr = cmd.ExecuteReader())
                            {
                                if (dr.Read())
                                {
                                    lstDownloaded.Items.Add(new DownloadedListAdapter((uint)(long)dr["ID"], (uint)(long)dr["SEASON"], (uint)(long)dr["EPISODE"], dr["DOWNLOADDATE"].ToString()));
                                }
                            }
                        }
                        else
                        {
                            System.Windows.Forms.MessageBox.Show("This episode has already been downloaded");
                        }
                    }

                }
                catch (Exception)
                {
                }
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            DownloadedListAdapter obj = (DownloadedListAdapter)lstDownloaded.SelectedItem;
            if (obj != null)
            {
                lstDownloaded.Items.Remove(obj);

                using (IDbCommand cmd = DB.CreateCommand())
                {
                    cmd.CommandText = "delete from downloaded where id=?";
                    IDataParameter par = cmd.CreateParameter();
                    par.Value = obj.id;
                    cmd.Parameters.Add(par);
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }
}
